package com.esms.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.esms.service.ILogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.esms.dao.EmployeeMapper;
import com.esms.po.Employee;
import com.esms.po.Grand;
import com.esms.po.Sassessment;
import com.esms.po.Sevaluation;
import com.esms.service.impl.EmployeeSevaluationServerImpl;
import com.esms.vo.EmployeeSevaluationVO;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
 * @author 帅帅帅帅
 *
 */

@Controller
public class EmployeeSevaluationController {
	@Autowired
	ILogService logService;

	@Autowired
	private EmployeeSevaluationServerImpl employeeSevaluationService;
	
	@Autowired
    private EmployeeMapper employeeMapper = null;
	
    @RequestMapping("employeeSevaluation.do")
    @ResponseBody
    public EmployeeSevaluationVO findSelectiveGrand(
            @RequestParam(value="e_id", defaultValue="0") String fEid,
            HttpSession httpSession) throws Exception {

        Integer eId = (Integer) httpSession.getAttribute("employeeId");
        //pageNum:起始页面  pageSize:每页的大小
      //  PageHelper.startPage(pageNum,limit);
        //查找条件，一定要紧跟在startPage后
        
        List<Sevaluation> sevaluationsList = employeeSevaluationService.getEmployeeSevaluationList(eId,fEid);

        //设置前台需要的数据
        EmployeeSevaluationVO employeeSevaluationVO = new EmployeeSevaluationVO();
        employeeSevaluationVO.setCode(0);
        employeeSevaluationVO.setMsg("");
        employeeSevaluationVO.setCount(sevaluationsList.size());
        employeeSevaluationVO.setData(sevaluationsList);
        return employeeSevaluationVO;
    }
    
    @RequestMapping("updateSevaluation.do")
    @ResponseBody
    public EmployeeSevaluationVO updateSevaluation(
    		@RequestParam("list") String list,
            HttpSession httpSession) throws Exception {
    	int req = 0;
    	String reMsg;
    	ObjectMapper mapper = new ObjectMapper();
    	JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, Sevaluation.class);
    	List<Sevaluation> listSevaluation =  (List<Sevaluation>)mapper.readValue(list, jt);
    	/*我的工号查询*/
    	Integer eId = (Integer) httpSession.getAttribute("employeeId");
    	Employee employee = employeeMapper.selectByPrimaryKey(eId);
    	reMsg = "评价成功";
		logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
				"下级对上级评价成功");
    	String quarter = null;
        String systemTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis()));
        int year = Calendar.getInstance().get(Calendar.YEAR);
        int month = Calendar.getInstance().get(Calendar.MONTH)+1;
        if(month>=1 && month<=3) {
        	quarter = year + "01";
        }else if (month>=4 && month<=6) {
        	quarter = year + "02";
        }else if (month>=7 && month<=9) {
        	quarter = year + "03";
        }else if (month>=10 && month<=12) {
        	quarter = year + "04";
        }
        
        Sassessment sassessment = new Sassessment();
    	for(int i=0;i<listSevaluation.size();i++) {
    		Sevaluation sevaluation = listSevaluation.get(i);
    		/*输入分数判断*/
    		
    		/*设置同级评分表*/
    		sassessment.setsAuserid(employee.geteAccount());
    		sassessment.setsDuserid(sevaluation.geteAccount());
    		sassessment.setsCreateTime(systemTime);
    		sassessment.setsCreateQuarter(quarter);
    		sassessment.setsTargetid1(1);
    		sassessment.setsTargetid2(2);
    		sassessment.setsTargetid3(3);
    		sassessment.setsTargetid4(4);
    		sassessment.setsTargetid5(5);
    		sassessment.setsTargetid6(6);
    		sassessment.setsTargetid7(7);
    		sassessment.setsTargetid8(8);
    		sassessment.setsTargetid9(9);
    		sassessment.setsTargetid10(10);
    		sassessment.setsTargetid11(11);
    		sassessment.setsTargetid12(12);
    		sassessment.setsTargetid13(13);
    		sassessment.setsTargetid14(14);
    		sassessment.setsTargetid15(15);
    		sassessment.setsTargetid16(16);
    		sassessment.setsTargetid17(17);
    		sassessment.setsTargetid18(18);
    		sassessment.setsTargetid19(19);
    		sassessment.setsTargetid20(20);
    		sassessment.setsTargetscore1(sevaluation.getsTargetscore1());
    		sassessment.setsTargetscore2(sevaluation.getsTargetscore2());
    		sassessment.setsTargetscore3(sevaluation.getsTargetscore3());
    		sassessment.setsTargetscore4(sevaluation.getsTargetscore4());
    		sassessment.setsTargetscore5(sevaluation.getsTargetscore5());
    		sassessment.setsTargetscore6(sevaluation.getsTargetscore6());
    		sassessment.setsTargetscore7(sevaluation.getsTargetscore7());
    		sassessment.setsTargetscore8(sevaluation.getsTargetscore8());
    		sassessment.setsTargetscore9(sevaluation.getsTargetscore9());
    		sassessment.setsTargetscore10(sevaluation.getsTargetscore10());
    		sassessment.setsTargetscore11(sevaluation.getsTargetscore11());
    		sassessment.setsTargetscore12(sevaluation.getsTargetscore12());
    		sassessment.setsTargetscore13(sevaluation.getsTargetscore13());
    		sassessment.setsTargetscore14(sevaluation.getsTargetscore14());
    		sassessment.setsTargetscore15(sevaluation.getsTargetscore15());
    		sassessment.setsTargetscore16(sevaluation.getsTargetscore16());
    		sassessment.setsTargetscore17(sevaluation.getsTargetscore17());
    		sassessment.setsTargetscore18(sevaluation.getsTargetscore18());
    		sassessment.setsTargetscore19(sevaluation.getsTargetscore19());
    		sassessment.setsTargetscore20(sevaluation.getsTargetscore20());
    		int sum = sevaluation.getsTargetscore1() + sevaluation.getsTargetscore2()
    		 + sevaluation.getsTargetscore3() + sevaluation.getsTargetscore4()
    		 + sevaluation.getsTargetscore5() + sevaluation.getsTargetscore6() 
    		 + sevaluation.getsTargetscore7() + sevaluation.getsTargetscore8()
    		 + sevaluation.getsTargetscore9() + sevaluation.getsTargetscore10()
    		 + sevaluation.getsTargetscore11() + sevaluation.getsTargetscore12()
    		 + sevaluation.getsTargetscore13() + sevaluation.getsTargetscore14()
    		 + sevaluation.getsTargetscore15() + sevaluation.getsTargetscore16()
    		 + sevaluation.getsTargetscore17() + sevaluation.getsTargetscore18()
    		 + sevaluation.getsTargetscore19() + sevaluation.getsTargetscore20();
    		sassessment.setsTargetscoresum(sum);
    		
    		req = employeeSevaluationService.deleteSevaluation(employee.geteAccount(),sevaluation.geteAccount());
    		if(req < 0) {
       		 reMsg = "评价失败";
				logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
						"下级对上级评价失败");
    		}
    		req = employeeSevaluationService.insertSevaluation(sassessment);
    		if(req < 0) {
          		 reMsg = "评价失败";
				logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
						"下级对上级评价失败");
       		}
        }

        //设置前台需要的数据
    	EmployeeSevaluationVO employeeSevaluationVO = new EmployeeSevaluationVO();
    	employeeSevaluationVO.setCode(0);
    	employeeSevaluationVO.setMsg(reMsg);
    	employeeSevaluationVO.setData(listSevaluation);
        return employeeSevaluationVO;
    }
    public int checkNumber(Grand grand) {
    	return 0;
    }
}
